package com.handu.luna.core.domain.goods;


import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.criteria.*;

import com.handu.luna.core.domain.BasePersistentDomain;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.handu.luna.core.domain.search.BrandSearch;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.jpa.domain.Specification;

@Entity
public class Brand extends BasePersistentDomain<BrandRepository> {

	public enum BrandType{
		COMMON("自有品牌"),AGENT("代购品牌"),SULV("素缕品牌");	
	    public String description;	    
	    BrandType(final String description) {
	        this.description = description;
	    }
	}	
	
	@NotNull(message="品牌长度不能超过30字")
    @Size(min=1,max=30, message="品牌长度不能为空或超过30字。")
	private String name;
	
	private String code;
	
	private String logo;
	@Size(max=100,message="品牌关键字长度不能超过100字")
	private String keyWord;
	@Lob
	private String story;
	@Size(max=200,message="品牌描述长度不能超过200字")
	private String description;
	@Size(max=200,message="品牌网址长度不能超过200字")
	private String url;
	@NotNull(message = "显示顺序必填，且只能是数字")
	@Max(value = 254,message = "显示顺序最大为254")
	@Min(value = 0 ,message = "显示顺序最小为0")
	private Integer displayOrder = 100;
	private boolean displayed = true;
	@NotNull
	private BrandType type;

	public Brand(){}

	public Brand(String name, int displayOrder) {
		super();
		this.name = name;
		this.displayOrder = displayOrder;
		this.displayed = true;
		this.url = "";
		this.description = "";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Integer getDisplayOrder() {
		return displayOrder;
	}

	public void setDisplayOrder(Integer displayOrder) {
		this.displayOrder = displayOrder;
	}

	public boolean isDisplayed() {
		return displayed;
	}

	public void setDisplayed(boolean displayed) {
		this.displayed = displayed;
	}
	
	public BrandType getType() {
		return type;
	}

	public void setType(BrandType type) {
		this.type = type;
	}
	
	

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getLogo() {
		return logo;
	}

	public void setLogo(String logo) {
		this.logo = logo;
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	public String getStory() {
		return story;
	}

	public void setStory(String story) {
		this.story = story;
	}

	public Brand(String name, String description, String url, int displayOrder,
			boolean displayed, BrandType type) {
		super();
		this.name = name;
		this.description = description;
		this.url = url;
		this.displayOrder = displayOrder;
		this.displayed = displayed;
		this.type = type;
	}

	public Brand(String name, String description, String url, int displayOrder,
			boolean isShow) {
		super();
		this.name = name;
		this.description = description;
		this.url = url;
		this.displayOrder = displayOrder;
		this.displayed = isShow;
		this.type = BrandType.COMMON;
	}
	
	public void delete(){
		context.getBrandRepository().delete(this.getId());
		List<BrandGeneralInfo> infos = context.getBrandGeneralInfoRepository().findByBrandId(this.getId());
		if(infos != null && infos.size() != 0){
			context.getBrandGeneralInfoRepository().delete(infos);
		}
	}

    public static Specification<Brand> searchCondition(BrandSearch brandSearch){
        return new Specification<Brand>() {
            @Override
            public Predicate toPredicate(Root<Brand> root, CriteriaQuery<?> query, CriteriaBuilder cb) {

                Predicate predicate = cb.conjunction();
                List<Expression<Boolean>> expressions = predicate.getExpressions();

                if (StringUtils.isNotBlank(brandSearch.getBrandname())){
                    expressions.add(cb.like(root.<String>get("name"),"%"+brandSearch.getBrandname()+"%"));
                }

                if (StringUtils.isNotBlank(brandSearch.getBrandtype())){
                    expressions.add(cb.equal(root.<BrandType>get("type"),BrandType.valueOf(brandSearch.getBrandtype())));
                }
                return query.where(predicate).getRestriction();
            }
        };
    }
	
	public static Specification<Brand> nameLike(String searchWord) {
		return new Specification<Brand>() {
			@Override
			public Predicate toPredicate(Root<Brand> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				if(searchWord != null){
					query.where(builder.and(builder.not(root.get("deleted")),builder.like(root.get("name").as(String.class),"%"+searchWord+"%")));
				}else{
					query.where(builder.not(root.get("deleted")));
				}
				query.orderBy(builder.asc(root.get("displayOrder")));
				return query.getRestriction();
			}
		};
	}
}
